using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Drawing;

namespace Possan.Graphics.Filters
{
	public class DropShadow : IBaseFilter
	{
		public string ParameterDefinition 
		{
			get
			{
				     return null;
			}
		}

		public int Distance;
		public int Angle;
		public int Radius;
		public int Opacity;

		public DropShadow()
		{
			Distance = 3;
			Angle = 90; // down
			Radius = 6;
			Opacity = 50;
		}

		public Bitmap Filter(Bitmap bmp)
		{
			FillColor fc = new FillColor();
			fc.Color = "#000000"; //
			Bitmap shadow = fc.Filter(bmp);

			BlurFilter bl = new BlurFilter();
			bl.Radius = Radius;
			shadow = bl.Filter(shadow);

			SetOpacity so = new SetOpacity();
			so.Opacity = Opacity;
			shadow = so.Filter(shadow);

			int nw = bmp.Width + (Radius * 2) + (Distance * 2);
			int nh = bmp.Height + (Radius * 2) + (Distance * 2);
			System.Drawing.Graphics g1 = System.Drawing.Graphics.FromImage(bmp);
			Bitmap bmp2 = new Bitmap(nw, nh);
			bmp2.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

			System.Drawing.Graphics g2 = System.Drawing.Graphics.FromImage(bmp2);

			int imgx = Radius + Distance;
			int imgy = Radius + Distance;

			double rad = (double)Angle * Math.PI / 180.0;
			int uoffset = (int)((float)Distance * (float)Math.Cos(rad));
			int voffset = (int)((float)Distance * (float)Math.Sin(rad));

			g2.DrawImage(shadow, (nw / 2) - (shadow.Width / 2) + uoffset, (nh / 2) - (shadow.Height / 2) + voffset);

			// add original on top
			g2.DrawImage(bmp, (nw / 2) - (bmp.Width / 2), (nh / 2) - (bmp.Height / 2));

			/*
			
	   Graphics g2 = Graphics.FromImage(bmp2);

	   int imgx = Radius + Distance;
	   int imgy = Radius + Distance;

		
	   // supersample shadow
	   g2.DrawLine(new Pen(Color.Red), new Point(0, 0), new Point(nw, nh));

	   float udelta = (float)Distance * (float)Math.Sin((float)Angle * Math.PI / 180.0f);
	   float vdelta = (float)Distance * (float)Math.Cos((float)Angle * Math.PI / 180.0f);

	   int samples = Radius * Radius;
	   int[] usamp = new int[samples];
	   int[] vsamp = new int[samples];
	   float[] weight = new float[samples];
	   Random r = new Random();
	   int s = 0;
	   for (int s1 = 0; s1 < Radius; s1++)
	   {
		   for (int s2 = 0; s2 < Radius; s2++)
		   {
			   float dx = ((s2 / (float)Radius) * 2.0f) - 1.0f;//(s2 * 2) - s2;// (float)((r.NextDouble() * 2.0) - 1.0);
			   float dy = ((s1 / (float)Radius) * 2.0f) - 1.0f;// 1 (s1 * 2) - s1;// (float)((r.NextDouble() * 2.0) - 1.0);

			   float x = udelta + (float)Radius * dx;
			   float y = vdelta + (float)Radius * dy;

			   usamp[s] = (int)x;
			   vsamp[s] = (int)y;
			   weight[s] = 1.0f / (1.0f + (float)Math.Sqrt(dx * dx + dy * dy));
			   s++;
		   }
	   }

	   for (int v = 0; v < bmp2.Height; v++)
	   {
		   for (int u = 0; u < bmp2.Width; u++)
		   {
			   //

			   float avgalpha = 0.0f;

			   for (int ss = 0; ss < samples; ss++)
			   {
				   int sx = u + usamp[ss] - imgx;
				   int sy = v + vsamp[ss] - imgy;
				   if (sx >= 0 && sy >= 0 && sx < bmp.Width && sy < bmp.Height)
				   {
					   Color c = bmp.GetPixel(sx, sy);
					   avgalpha += (float)c.A * weight[ss];
				   }
			   }

			   avgalpha /= (float)samples;
			   bmp2.SetPixel(u, v, Color.FromArgb((int)avgalpha, Color.Black));
		   }
	   }

	   // add original on top
	   g2.DrawImage(bmp, imgx, imgy);
									   */
			return bmp2;
		}

		public void Dispose()
		{
		}
	}
}





